// -*- c++ -*-
/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright (c) 1999-2003, Mercury Computer Systems, Inc.,       */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#ifndef AUDIO_IO_H__
#define AUDIO_IO_H__

#include <vector>
#include <fstream>
#include <iostream>

#include "CF__Resource_impl.h"
#include "AudioPort_impl.h"
#include "SceOsThreadManager.h"
#include "mc_error.h"

#include <windows.h>
#include <mmsystem.h>

typedef std::vector<double> FVector;
typedef std::vector<long>  LVector;
typedef FVector::iterator FVecIt;
typedef LVector::iterator LVecIt;


//Bit Manipulation stuff
#define CBIT(w, b)	((w) &= ~(0x01 << (b)))
#define SBIT(w, b)	((w) |= (0x01 << (b)))
#define TBIT(w, b)	((((w) >> (b)) & 0x01) ? 1 : 0)

#define NBUFFERS    12
#define BLOCKSIZE   640

#ifdef _WIN32
#ifndef AUDIO_EXPORTS
#define AUDIO_EXPORT __declspec(dllimport)
#else 
#define AUDIO_EXPORT __declspec(dllexport)
#endif
#else //_WIN32
#define AUDIO_EXPORT
#endif // _WIN32


struct audio_prop_struct {
  long frequency;
  long devid;
};

typedef struct audio_prop_struct audio_properties;


class AUDIO_EXPORT AudioComponent : public CF__Resource_impl
{
public:
  virtual ~AudioComponent();

protected:
  AudioComponent (const char * identifier);
  status Init ();

  // Inner loop method that performs component business logic
  virtual status Run () = 0;
  // Separate thread master function	
  static void RunWorker (void * param);

  // Update properties using base class implementation
  void UpdateAudioProperties ();

  double m_fAverageRMS;
  int m_iOldbits;

  double m_fOld_avg_delta;

  short * m_vlPCMData;
  float * m_vfPCMData;
  static const int FRAME_BLK_SIZE;
  static const int FILTER_LENGTH;

  bool  m_bStop;
  int   m_iLineNumber;

  WAVEHDR m_audiohdr [NBUFFERS];

  SCE::OS::ThreadManager m_thread;
  bool m_threadrunning;

  bool  m_recording;
  long m_cbuffer;

  audio_properties * m_props;
  CF::Properties m_cf_props;
};

/*
 * Audio Capture component.  This component takes audio input from 
 * a microphone and streams the data out for playback and/or processing.
 *
 * This component implementation uses Win32 SDK calls which are not
 * part of the SCA AEP.
 */

class AUDIO_EXPORT AudioCapture : public AudioComponent {
public:
  static status Create (AudioCapture *& Handle, const char * identifier);
  virtual ~AudioCapture ();

  status Init ();

  // From CF::Resource
  void start ()
    throw (CORBA::SystemException, CF::Resource::StartError);
  void stop ()
    throw (CORBA::SystemException, CF::Resource::StopError);

  // From CF::PortSupplier
  virtual CORBA::Object_ptr getPort (const CORBA::Char * name)
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

protected:
  virtual status Run ();

private:
  AudioCapture (const char * identifier);

  PcmOutPort_impl *m_pcmout;  
  HWAVEIN m_lphwi;
  char m_audio[NBUFFERS][BLOCKSIZE];
};

/*
 * Audio Playback component.  This component takes audio input data
 * and plays the audio on the PC sound ouput.
 *
 * This component implementation uses Win32 SDK calls which are not
 * part of the SCA AEP.
 */

class AUDIO_EXPORT AudioPlayback : public AudioComponent {
public:
  static status Create (AudioPlayback *& Handle, const char * identifier);
  virtual ~AudioPlayback ();

  status Init ();

  // From CF::Resource
  void start ()
    throw (CORBA::SystemException, CF::Resource::StartError);
  void stop ()
    throw (CORBA::SystemException, CF::Resource::StopError);

  // From CF::PortSupplier
  virtual CORBA::Object_ptr getPort (const CORBA::Char * name)
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

protected:
  virtual status Run ();

private:
  AudioPlayback (const char * identifier);
  status ConvertToAudio (int length);

  PcmInPort_impl * m_pcmin;
  HWAVEOUT m_lphwo;
  short m_audio[NBUFFERS][BLOCKSIZE];
};

#endif // AUDIO_IO_H__
